package com.molyfun.parents.activity.record.itemview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.RecordResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/7/16.
 */
public class BaseRecordView extends LinearLayout implements View.OnLongClickListener ,View.OnClickListener{

    protected RecordResponse.RecordBean mRecordBean;

    public BaseRecordView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public BaseRecordView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseRecordView(Context context) {
        super(context);
    }

    @Override
    public boolean onLongClick(View v) {
        showPopupWindow(v);
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.record_delete:
                if(mRecordBean != null){
                    deleteRecord();
                }
                popupWindow.dismiss();
                break;
        }
    }

    PopupWindow popupWindow;
    TextView mRecordDel;

    protected void showPopupWindow(View view) {

        if (mRecordBean == null) {
            return;
        }
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.view_record_popup_menu, null);
        mRecordDel = (TextView) contentView.findViewById(R.id.record_delete);


        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mRecordDel.setOnClickListener(this);

        int[] location = new int[2];
        view.getLocationOnScreen(location);

        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0] + popupWindow.getWidth() + view.getWidth(), location[1] + (view.getHeight() >> 1));

//        popupWindow.showAsDropDown(view);//, Gravity.NO_GRAVITY, 0, 0);
    }

    protected RecordOperationCallBack nRecordOperationCallBack;

    public void setRecordOperationCallBack(RecordOperationCallBack recordOperationCallBack){
        nRecordOperationCallBack = recordOperationCallBack;
    }

    public interface RecordOperationCallBack{
        void onRecordDel(RecordResponse.RecordBean recordBean);
        void onRecordReUpload(RecordResponse.RecordBean recordBean);
    }

    private void deleteRecord(){
        if(!TextUtils.isEmpty(mRecordBean.getId())){
            Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().deleteRecord(mRecordBean.getId());
            call.enqueue(new Callback<BaseResponse>() {
                @Override
                public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                    if(nRecordOperationCallBack != null){
                        nRecordOperationCallBack.onRecordDel(mRecordBean);
                    }
                }

                @Override
                public void onFailure(Call<BaseResponse> call, Throwable t) {
                }
            });
        }else{
            if(nRecordOperationCallBack != null){
                nRecordOperationCallBack.onRecordDel(mRecordBean);
            }
        }
    }

}
